import permision from "./permission.js"
let lock = true
const checkGpsIsOpen = async (mes) => {
	//加锁防止重复的请求
	console.log('检查定位设置开启问题', permision.checkSystemEnableLocation());
	if (lock) {
		lock = false
		if (!permision.checkSystemEnableLocation()) {
			plus.nativeUI.confirm("手机定位权限没有开启，是否去开启？", (e) => {
				if (e.index == 0) {
					if (uni.getSystemInfoSync().platform == "ios") {
						plus.runtime.openURL("app-settings://");
					} else {
						var main = plus.android.runtimeMainActivity(); //获取activity
						var Intent = plus.android.importClass('android.content.Intent');
						var Settings = plus.android.importClass('android.provider.Settings');
						var intent = new Intent(Settings
							.ACTION_LOCATION_SOURCE_SETTINGS); //可设置表中所有Action字段  
						main.startActivity(intent);
					}
					lock = true
				} else {
					
					uni.showToast({
						title: '设备无定位权限',
						icon: 'none'
					});
					lock = true
				}
			}, {
				"buttons": ["去设置", "不开启"],
				"verticalAlign": "center"
			});
			return false
		}
		let checkAppGpsRes = await checkAppGps()
		console.log(checkAppGpsRes, 'checkAppGpsRes');
		if (!checkAppGpsRes) {
			plus.nativeUI.confirm("应用定位权限没有开启，是否去开启？", (e) => {
				lock = false
				if (e.index == 0) {
					permision.gotoAppPermissionSetting()
					lock = true
				} else {
					lock = true
					uni.showToast({
						title: '应用无定位权限',
						icon: 'none'
					});
				}
			}, {
				"buttons": ["去设置", "不开启"],
				"verticalAlign": "center"
			});
		} else {
			lock = true
		}
	}
}

const checkAppGps = async function() {
	if (uni.getSystemInfoSync().platform == "ios" && !permision.judgeIosPermission("location")) {
		return false
	}
	if (uni.getSystemInfoSync().platform != "ios" && await permision.requestAndroidPermission(
			"android.permission.ACCESS_FINE_LOCATION") != 1) {
		return false
	}
	return true
}
export default {
	checkAppGps,
	checkGpsIsOpen
}
